package com.atom.module.lib.opengles.shader

import com.atom.module.lib.opengles.draw.GlDrawable
import com.atom.module.lib.opengles.util.MatrixUtils

class GlScene : GlViewportAware() {

    val projectionMatrix: FloatArray = MatrixUtils.IDENTITY_MATRIX.clone()
    val viewMatrix: FloatArray = MatrixUtils.IDENTITY_MATRIX.clone()

    private val modelViewMatrix = FloatArray(16)
    private val modelViewProjectionMatrix = FloatArray(16)

    private fun computeModelViewProjectionMatrix(drawable: GlDrawable) {
        MatrixUtils.matrixMultiply(modelViewMatrix, viewMatrix, drawable.modelMatrix)
        MatrixUtils.matrixMultiply(modelViewProjectionMatrix, projectionMatrix, modelViewMatrix)
    }

    fun draw(program: GlProgram, drawable: GlDrawable) {
        ensureViewportSize()
        drawable.setViewportSize(viewportWidth, viewportHeight)
        computeModelViewProjectionMatrix(drawable)
        program.draw(drawable, modelViewProjectionMatrix)
    }
}